package com.imut.onlinebanking.user.service.impl;

import com.imut.onlinebanking.user.mapper.LoginMapper;
import com.imut.onlinebanking.user.mapper.UserMapper;
import com.imut.onlinebanking.user.pojo.entity.User;
import com.imut.onlinebanking.user.pojo.vo.UserVO;
import com.imut.onlinebanking.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用来实现逻辑层
 * 相当于servlet
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO selectUserByName(User user) {
        System.out.println("selectUserByName: " + user);
        UserVO userVO = loginMapper.selectUserByName(user);
        System.out.println("userVO: " + userVO);
        return userVO;
    }

    @Override
    public boolean updateAddress(User user) {
        int rows = loginMapper.updateAddress(user);
        return rows > 0;
    }

    @Override
    public void updateUser(User user) throws Exception {
        System.out.println("user = " + user);
        int r = userMapper.updateUser(user);
        System.out.println("r = " + r);
        if (r <= 0){
            throw new Exception("更新用户信息失败");
        }else {
            System.out.println("更新用户信息成功");
        }

    }
    @Override
public void deleteUserById(Integer userId) {
    int rows = userMapper.deleteUserById(userId);
    if (rows <= 0) {
        throw new RuntimeException("删除用户失败");
    }
}

}
